<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="model.pojo.*, java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <%
            User ou = (User) request.getAttribute("ownerOfPost");
            User cu = (User) request.getAttribute("currentUser");
            request.setAttribute("pageTitle", ou.getDisplayName() + "'s Blog");
    %>
    <%@ include file="/WEB-INF/jspf/header.jspf"%>
    
    <%
            if (request.getAttribute("post") != null) {
                Post p = (Post) request.getAttribute("post");

    %>
    <div class="portion"> 
    <table align="center" width="80%">
        <tbody>
            <div class="post">
            <tr>
                <td><h3><a class="post" href=<%=response.encodeURL("singlepost.do?postId=" + p.getPostId())%>><%= p.getPostTitle()%> - <%=p.getUserID().getDisplayName()%></a></h3></td>
            </tr>
            <tr>
                <td>
                    <%= p.getPostText()%>
                </td>
            </tr>
        </div>
            <%

        if (cu != null) {

            //show the delete option only if the currently viewing user is the author of the post
            if (cu.equals(p.getUserID()) || fbUser.isAdmin(cu)) {
            %>
            <tr>
                <td align="center">
                    <%
                        if(cu.equals(p.getUserID())){
                    %>
                    <a class="minor" href=<%=response.encodeURL("editExistingPost.do?postId=" + p.getPostId())%>>Edit this post</a>
                    <%
                        }
                    %>
                    <a class="minor" href=<%=response.encodeURL("removePost.do?postId=" + p.getPostId())%>>Delete this post</a>
                </td>
            </tr>
            <%
                        }
            %>
            <tr>
                <td>
                    <form action="addComment.do" method="POST">
                        <table align="center" width="80%">
                            <tbody>
                                <tr align="center">
                                    <td>
                                        <textarea name="commentText" rows="5" cols="50" style="width: 80%"></textarea>
                                        <input type="hidden" name="postId" value="<%= p.getPostId()%>" />
                                    </td>
                                </tr>
                                <tr align="center">
                                    <td>
                                        <input align="right" type="submit" value="Add Comment" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </form>
                </td>
            </tr>
            <br>
            <%
        }
        Collection<Comment> clist = (Collection<Comment>) request.getAttribute("comments");
        if ((clist != null) && (clist.size() > 0)) {
            %>
            <tr>
                <td><I>Comments</I></td>
            </tr>
            
            <tr>
                <td>
                    <table align="center" border="0">
                        <tbody>
                            <%
                for (Comment c : clist) {
                            %>
                            
                            <tr>
                                <td><I><B><%= c.getUserId().getDisplayName()%></B> says:</I>
                                    <br>
                                    <I><%= c.getCommentText()%></I>
                                </td>
                            </tr>
                            <%
                                if (cu != null) {
                                    if (p.getUserID().equals(cu) || c.getUserId().equals(cu)) {
                            %>
                            <tr>
                                <td align="center">
                                    <a class="minor" href=<%=response.encodeURL("removeComment.do?commentId=" + c.getCommentId())%>>Delete this comment</a>
                                </td>
                            </tr>
                            <%
                        }
                    }
                }
                            %>
                        </tbody>
                    </table>
                </td>
            </tr>
            <%
        }
            %>
            
        </tbody>
    </table>
</div>
    <%

    } else {
    %>
    
    Error viewing the post... please retry
    
    <%            }
    %>
    
    <%@ include file="/WEB-INF/jspf/footer.jspf"%>
</html>
